package cn.openea.perpartner.meily.web.controller;

import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.web.controller.BaseController;
import cn.openea.perpartner.common.core.web.domain.AjaxResult;
import cn.openea.perpartner.common.core.web.page.TableDataInfo;
import cn.openea.perpartner.meily.web.entity.ProvideSamples;
import cn.openea.perpartner.meily.web.entity.dto.ProvideSamplesDTOFactory;
import cn.openea.perpartner.meily.web.entity.vo.ProvideSamplesSubVo;
import cn.openea.perpartner.meily.web.entity.vo.ProvideSamplesVo;
import cn.openea.perpartner.meily.web.service.IProvideSamplesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author ysk
 * @email zane_ysk@qq.com
 * @date 2023-03-27 11:37:04
 */
@Api(tags = "样品发放管理")
@RestController
@RequestMapping("/provide/samples")
public class ProvideSamplesController extends BaseController {

    @Resource
    private IProvideSamplesService provideSamplesService;
    @Resource
    private ProvideSamplesDTOFactory provideSamplesDTOFactory;

    @ApiOperation(value = "发布样品发放活动")
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody @Valid ProvideSamples provideSamples) {
        return this.provideSamplesService.publish(provideSamples);
    }

    @ApiOperation(value = "获取样品发放详情")
    @GetMapping("/info/{id}")
    public AjaxResult info(@PathVariable String id) {
        return this.provideSamplesService.queryById(id);
    }

    @ApiOperation(value = "获取样品发放列表(分页)")
    @GetMapping("/list")
    public TableDataInfo list(ProvideSamplesVo vo) {
        startPage();
        List<ProvideSamples> samplesList = provideSamplesService.findListByVo(vo);
        TableDataInfo dataTable = getDataTable(samplesList);
        List<ProvideSamplesDTOFactory.InfoAndIndicatorsDTO> infoAndIndicatorsDTOS = samplesList.parallelStream()
                .map(item -> provideSamplesDTOFactory.toInfoAndIndicatorsDTO.apply(item))
                .collect(Collectors.toList());
        dataTable.setRows(infoAndIndicatorsDTOS);
        return dataTable;
    }

    @ApiOperation(value = "获取样品发放列表-小程序")
    @PostMapping("/getAllList")
    public TableDataInfo getAllList(@RequestBody ProvideSamplesSubVo vo) {
        startPage();
        List<ProvideSamples> list = provideSamplesService.getAllList(vo);
        return getDataTable(list);
    }

    @ApiOperation(value = "活动作废")
    @PostMapping("/finish")
    public AjaxResult finish(@RequestBody ProvideSamplesVo vo) {
        if(!this.provideSamplesService.finish(vo)){
            return AjaxResult.error("作废活动失败！");
        }
        return AjaxResult.success("作废活动成功！", provideSamplesService.getById(vo.getId()));
    }

    @ApiOperation(value = "活动结束报名")
    @PostMapping("/end/join")
    public AjaxResult endJoin(@RequestBody ProvideSamplesVo vo) {
        if(!this.provideSamplesService.endJoin(vo)){
            return AjaxResult.error("活动结束报名失败！");
        }
        return AjaxResult.success("活动结束报名成功！", provideSamplesService.getById(vo.getId()));
    }

    @ApiOperation(value = "根据订单id获取样品发放详情")
    @GetMapping("/info/byOrderId/{orderId}")
    public AjaxResult infoByOrderId(@PathVariable String orderId) {
        if (StrUtil.isBlank(orderId)) {
            return AjaxResult.error("获取不到orderId！");
        }
        LambdaQueryWrapper<ProvideSamples> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProvideSamples::getOrderId, orderId);
        return AjaxResult.success("获取样品发放详情成功！", this.provideSamplesService.getOne(wrapper));
    }

}
